import moment from "moment"
const formats = {
    date: "YYYY-MM-DD",
    datetime: "YYYY-MM-DD HH:mm:ss",
    minute: "YYYY-MM-DD HH:mm",
    month: "MM-DD",
    year: "YYYY",
    year_month: "YYYY-MM",
    hour: "HH:mm:ss",
    hm: "HH:mm",
    mdhm: "MM月DD日 HH:mm",
    md: "MM月DD日"
}
let timeout
export default {
    // 基础版的防抖函数
    debounce(func, wait) {
        return function () {
            clearTimeout(timeout)
            timeout = setTimeout(func, wait)
        }
    },
    // 基础版的节流函数
    throttle(func, wait) {
        return function () {
            if (!timeout) {
                timeout = setTimeout(function () {
                    clearTimeout(timeout)
                    timeout = null
                    func()
                }, wait)
            }
        }
    },
    navPage(url) {
        uni.navigateTo({ url })
    },
    navReLaunchPage(url) {
        uni.redirectTo({ url })
    },
    navRedirectPage(url) {
        uni.redirectTo({ url })
    },
    navTabPage(url) {
        uni.switchTab({ url })
    },
    navBack() {
        uni.navigateBack({
            delta: 1 //返回的页面数，如果 delta 大于现有页面数，则返回到首页,
        })
    },
    pages() {
        let pages = getCurrentPages()
        let curPage = pages[pages.length - 1]
        //在微信小程序或是app中，通过curPage.options；如果是H5，则需要curPage.$route.query（H5中的curPage.options为undefined，所以刚好就不需要条件编译了）
        return curPage.options || curPage.$route.query
    },
    formatDate(value, format = "date") {
        if (!value) return ""
        const pattern = formats[format]
        return moment(Number(value)).format(pattern)
    },
    setTime(time = 50) {
        return new Promise(resolve => {
            setTimeout(() => {
                resolve()
            }, time)
        })
    },
    //提示
    showToast(text) {
        uni.showToast({
            title: text,
            icon: "none"
        })
    },
    //显示加载中
    showLoading(text) {
        uni.showLoading({
            title: text,
            mask: true
        })
    },
    // 显示成功提示
    showSuccess(text) {
        uni.showToast({
            title: text,
            icon: "success"
        })
    },
    showWaiting(text, duration = 0) {
        return new Promise(async resolve => {
            await this.$setTime(300)
            await uni.showToast({ title: text, icon: "success" })
            await this.$setTime(duration)
            resolve()
        })
    },
    // 弹框提示
    showModal(content, showCancel = true) {
        return new Promise(reslove => {
            uni.showModal({
                title: "温馨提示",
                content: content,
                showCancel,
                success: res => {
                    reslove(res.confirm)
                }
            })
        })
    },
    // 下拉弹框
    showActionSheet(list, color = "#333") {
        return new Promise(reslove => {
            uni.showActionSheet({
                itemList: list,
                itemColor: color,
                success: res => {
                    reslove(res)
                }
            })
        })
    },
    //获取手机信息
    getSystemInfo() {
        return new Promise(reslove => {
            uni.getSystemInfo({
                success: res => {
                    console.log(res)
                    res.isIOS = res.platform === "ios" ? 1 : 0
                    reslove(res)
                }
            })
        })
    },
    countAcitivityStatus(date) {
        if (date > this.$formatDate(new Date(), "date")) {
            return "未开始"
        }
        if (date === this.$formatDate(new Date(), "date")) {
            return "进行中"
        }
        return "已结束"
    }
}
